package ejemplo;

import javax.swing.table.*;
import java.sql.*;

public class MyTableModel extends AbstractTableModel {

    ResultSet rs;

    public MyTableModel(){ }

    public MyTableModel(ResultSet rs){ this.rs=rs; }

    public String getColumnName(int column) {
        String name=null;
        try{  
            ResultSetMetaData rm = rs.getMetaData(); 
            name = rm.getColumnName(column+1);
        }catch(SQLException e){ e.printStackTrace();}
        finally{ return name;}
    }

    public int getRowCount() {
       int rcount=0;
       try{
           rs.last();
           rcount = rs.getRow();
       }catch(SQLException e){ e.printStackTrace();}
        finally{
            return rcount;}
    }

    public int getColumnCount() {
        int ccount=0;
        try{
            ResultSetMetaData rm = rs.getMetaData();
            ccount = rm.getColumnCount();
        } catch(SQLException e){ e.printStackTrace();}
       finally{
          return ccount;
       }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Object obj=null;
        try{
            rs.absolute(rowIndex+1);
            obj = rs.getObject(columnIndex+1);
        }catch(SQLException e){ e.printStackTrace();}
        finally{
          return obj;
        }

    }

    public Class<?> getColumnClass(int column){
         Class<?> c=null;
         try{
             ResultSetMetaData rm = rs.getMetaData();
             String name = rm.getColumnClassName(column+1);
             c=Class.forName(name);
         }catch(SQLException e){ e.printStackTrace();}
         finally{
         return c;
         }
    }

}
